package com.sgb.kjwl.view.ui.activity.self;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityEditOilBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.OilInfoEntity;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.sgb.kjwl.viewmodel.self.EditeOilModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.MToast;

import java.util.List;

import javax.inject.Inject;

public class EditOilActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {
    ActivityEditOilBinding binding;
    @Inject
    EditeOilModel model;
    private List<CarriageSelectEntity> brandList;
    private int brandPosition;
    private List<CarriageSelectEntity> typeList;
    private int typePosition;
    String oilTypeNo,brandNo;
    int id;
    public static void start(Context context) {
        Intent intent = new Intent(context, EditOilActivity.class);

        context.startActivity(intent);
    }
    public static void start(Context context,   OilInfoEntity entity) {
        Intent intent = new Intent(context, EditOilActivity.class);
        intent.putExtra("info",entity);
        context.startActivity(intent);
    }
    @Override
    protected void initView() {
        binding= DataBindingUtil.setContentView(this, R.layout.activity_edit_oil);
        binding.include.titleView.setText("新增油品");
        binding.include.ivBack.setOnClickListener(this);
        binding.itemLogo.setOnWholeItemClickListener(this);
        binding.itemType.setOnWholeItemClickListener(this);
        binding.nextView.setOnClickListener(this);
        binding.itemPrice.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemPrice2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
       OilInfoEntity info= (OilInfoEntity) getIntent().getSerializableExtra("info");
       if(info!=null){
         binding.setViewModel(info);
         id=info.getId();
         oilTypeNo=info.getOilTypeNo();
         brandNo=info.getBrandNo();
         binding.itemLogo.setContent(info.getBrandName());
         binding.itemType.setContent(info.getOilTypeName());
         binding.itemPrice.setContent(info.getPrice());
         binding.itemPrice2.setContent(info.getStandardPrice());
       }
    }

    @Override
    protected void init() {

    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.next_view:
                if(TextUtils.isEmpty(binding.itemLogo.getContent())){
                    MToast.showToast(getActivity(),"请选择品牌");
                return;
            }
                if(TextUtils.isEmpty(binding.itemType.getContent())){
                    MToast.showToast(getActivity(),"请选择油类型");
                    return;
                }
                if(TextUtils.isEmpty(binding.itemPrice.getContent())){
                    MToast.showToast(getActivity(),"请输入价格");
                    return;
                }
                if(TextUtils.isEmpty(binding.itemPrice2.getContent())){
                    MToast.showToast(getActivity(),"请输入标准价格");
                    return;
                }
                model.saveOil(id,oilTypeNo,brandNo,Double.parseDouble(binding.itemPrice.getContent()),Double.parseDouble(binding.itemPrice2.getContent()));
                break;


        }
    }

    //油品牌
    public void setBrandPop(List<CarriageSelectEntity> brandList) {
        this.brandList = brandList;
        showBrandPop();
    }

    private void showBrandPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            String content = brandList.get(options1).getName();
            binding.itemLogo.setContent(content);
            brandPosition = options1;
            brandNo=brandList.get(options1).getNo();

        }).build();
        businessOption.setPicker(brandList);
        businessOption.setSelectOptions(brandPosition);
        businessOption.show();
    }

    //油类型
    public void setTypePop(List<CarriageSelectEntity> typeList) {
        this.typeList = typeList;
        showCarLengthPop();
    }

    private void showCarLengthPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            String content = typeList.get(options1).getName();
            binding.itemType.setContent(content);
            typePosition = options1;
            oilTypeNo=typeList.get(options1).getNo();

        }).build();
        businessOption.setPicker(typeList);
        businessOption.setSelectOptions(typePosition);
        businessOption.show();
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()){
            case R.id.item_logo:
                if(brandList==null){
                    model.oilBrand();
                }else {
                    showBrandPop();
                }
                break;
            case R.id.item_type:
                if(typeList==null){
                    model.oilType();

                }else{
                    showCarLengthPop();
                }
                break;
        }

    }


}
